home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 18253 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.9 KB

  1. Path: news.nask.org.pl!usenet
  2. From: Roman Habrat <romek@robix.comp.waw.pl>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: BC 3.1 - 4.5 (error!?) ATTENTION - AVOID TRICKS
  5. Date: Fri, 19 Apr 1996 18:19:57 +0200
  6. Organization: Comp SA
  7. Message-ID: <3177BD2D.246B@robix.comp.waw.pl>
  8. References: <3173D628.48F6@demos.su> <4l5gn1$gtb@Grouper.Exis.Net> <31768AA5.420E@dbs.demos.su>
  9. NNTP-Posting-Host: robix.comp.waw.pl
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 3.0b2 (X11; I; SunOS 5.3 sun4m)
  14. CC: 00alex@dbs.demos.su
  15.  
  16. Alexey Ruzin wrote:
  17.  
  18. > > >Here is simple example of abnormal programming.
  19. > > >But BC crashed (sorry, it works and even compiles
  20. > > >the program, but it is unpredictable what will occur)
  21. > > >on it:
  22.  
  23. and later explained:
  24.  
  25. > Thanks to all, who reply on my message. I think i need to
  26. > explain some things in hronological sequence.
  27. > Once my friend wrote a program. He need to store incoming value
  28. > in some function at first call and do something else with
  29. > this value at next calls. He wants initialize 'static' variable
  30. > with incoming value:
  31. > int func( int a )
  32. > {
  33. >         static int sv = a;
  34. > ...
  35. > }
  36.  
  37. That is pretty example of so called TRICK-PROGRAMMING (above).
  38. One tries to use tricks rather than write program clearly and reliably.
  39.  
  40. How to do it reliably:
  41.  
  42. int func (int a)
  43. {
  44.     static int ready = 0;    // or FALSE
  45.     static int sv;
  46.  
  47.     if (! ready)
  48.         {
  49.         sv = a;
  50.         ready = 1;           // or TRUE
  51.         }
  52. ...
  53. }
  54.  
  55. It works always and with every compiler. You don't waste time trying.
  56.  
  57. Remember:
  58. Even if you would test a trick with 20 compilers, on 10 different
  59. machines, on 10 diferent operating systems, it can fail with
  60. 21th compiler, 11th machine, or simply with other compliler options
  61. (one optimization less or more for instance).
  62.  
  63. There is no ADDITIONAL execution overhead (for testing 'ready') nor
  64. for memory (for the flag), because it is INEVITABLE.
  65. What you think, how would the program perform exactly what you want
  66. without such an overhead ?
  67. Even if compiler would 'magically' prepare such a code, there always
  68. must be a flag (apparent or hidden): if it is a first call to the
  69. function or it is not.
  70. I am sure it's better to make such a flag apparent.
  71.  
  72. Of course, if values for 'a' are not from whole 'int' domain
  73. (but for instance -1000..1000), we can use another value as an
  74. sentinel:
  75.  
  76. #define OTHER 1001      // assumption: 'a' cannot take such a value
  77.  
  78. int func (int a)
  79. {
  80.     static int sv = OTHER;
  81.  
  82.     if (sv == OTHER)    // called first time ?
  83.         sv = a;
  84. ...
  85. }
  86.  
  87. But now we should comment that, because it is kind of a trick.
  88.  
  89. > (...)
  90. > Thanks to all for attention, that was my fault.
  91.  
  92. Don't worry Alexey.
  93. Usually better to learn something by experimentation,
  94. then blindly put such a code into big project without testing.
  95. You experimented and asked people here, it was good job.
  96.  
  97. ---
  98. Roman Habrat
  99.